package com.example.cadmin.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cadmin.entity.FileAttribute;
import com.example.cadmin.mapper.FileAttributeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cadmin.utils.DateFormat;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 档案属性 服务实现类
 * </p>
 *
 * @author zzsoft
 * @since 2023-02-13
 */
@Service
public class FileAttributeService extends ServiceImpl<FileAttributeMapper, FileAttribute> {

    @Resource
    private FileAttributeMapper fileAttributeMapper;

    /**
     * 档案属性列表
     *
     * @param fileAttributeName 文件属性名称
     * @param page              页码
     * @param limit             页数
     * @return Map<String, Object>
     */
    public Map<String, Object> attributeList(String fileAttributeName, int page, int limit) {
        //参数一是当前页，参数二是每页个数
        IPage<Map<String, Object>> attributePage = new Page<>(page, limit);
        QueryWrapper<FileAttribute> wrapper = new QueryWrapper<>();
        //判断一下是否有搜索条件
        if (StrUtil.isNotEmpty(fileAttributeName)) {
            wrapper.like("file_attribute_name", fileAttributeName);

        }
        attributePage = fileAttributeMapper.selectMapsPage(attributePage, wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", attributePage.getTotal());
        map.put("data", attributePage.getRecords());
        return map;
    }

    /**
     * 保存档案属性
     */
    public ResponseModel attributeSave(JSONObject data) {
        QueryWrapper<FileAttribute> queryWrapper = new QueryWrapper<FileAttribute>().eq("file_attribute_name",data.getStr("fileAttributeName"));
        FileAttribute fileAttributeExists = fileAttributeMapper.selectOne(queryWrapper);
        if (fileAttributeExists != null) {
            return ResponseModel.fail("档案属性名称已重复");
        }
        FileAttribute fileAttribute = new FileAttribute();
        fileAttribute.setFileAttributeName(data.getStr("fileAttributeName"));
        fileAttribute.setCreateTime(DateFormat.getTime());
        fileAttribute.setUpdateTime(DateFormat.getTime());
        int insertStatus = fileAttributeMapper.insert(fileAttribute);
        return insertStatus > 0 ? ResponseModel.success("保存档案属性成功") : ResponseModel.fail("保存档案属性失败");
    }

    /**
     * 修改档案属性
     */
    public ResponseModel editInfo(@RequestBody JSONObject data){
        QueryWrapper<FileAttribute> queryWrapper = new QueryWrapper<FileAttribute>().eq("file_attribute_name",data.getStr("fileAttributeName"));
        FileAttribute fileAttributeExists = fileAttributeMapper.selectOne(queryWrapper);
        if (fileAttributeExists != null) {
            return ResponseModel.fail("档案属性名称已重复");
        }
        UpdateWrapper<FileAttribute> updateWrapper = new UpdateWrapper<FileAttribute>().eq("id", data.getInt("fileAttributeId"))
                .set("file_attribute_name", data.getStr("fileAttributeName")).set("update_time", DateFormat.getTime());
        int updateStatus = fileAttributeMapper.update(null, updateWrapper);
        return updateStatus > 0 ? ResponseModel.success("修改档案属性成功") : ResponseModel.fail("修改档案属性失败");
    }

    /**
     * 删除档案属性
     */
    public ResponseModel remove(String attributeId) {
        int delStatus = fileAttributeMapper.deleteById(attributeId);
        return delStatus > 0 ? ResponseModel.success("档案属性删除成功") : ResponseModel.fail("档案属性删除失败");
    }

}
